<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 * 小程序端，小程序端不会自动发送cookie，需要手动发送sessionID，此处访问首页，成功则返回sessionID，失败则返回0
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'login';
if($_RQ['ttgid'] || $_RQ['wxgid'] || $_RQ['wxapp_id']){
	$wxapp_id=intval($_RQ['ttgid'])?:intval($_RQ['wxgid']);
	$wxapp_id=$wxapp_id?:intval($_RQ['wxapp_id']);
	$_SESSION['ttapp_id']=$_SESSION['appgid']=$wxapp_id;
	if(!$_SESSION['ttapp_id']){
		exit('error_ttapp:'.$_RQ['ttgid']);
	}
}else{
	return false;
	exi('小程序ID不存在！','error');
}
//H5小程序传openid
if($_RQ['app_openid'] && !$_SESSION['openid']){
	$_SESSION['openid']=$_RQ['app_openid'];
	$fans=pdo_get('core_fans',array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'ttgid'=>$_SESSION['ttapp_id']),array('id','mid'));
	if($fans['mid']){
		$mid= $fans['mid'];
	}
	if($mid){
		model('core/member')->login($mid);
	}
	return false;
}
$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['ttapp_id'],'uniacid'=>$_SESSION['uniacid']));
if(!$wxapp){
	exi('小程序不存在！','error');
}
if($_RQ['op']=='login'){
	if($_RQ['code']){
		//如果有code，根据code获取粉丝openid
		$url='https://developer.toutiao.com/api/apps/jscode2session?appid='.$wxapp['tt_appid'].'&secret='.$wxapp['tt_secret'].'&code='.$_RQ['code'].'&grant_type=authorization_code';
		$result=cfc('http')->ihttp_get($url);
		if(is_error($result)){
			exi('请求失败','error');
		}
		$result=json_decode($result['content'],true);
		if($result['errcode']){
			exi('获取抖音粉丝信息失败，错误码：'.$result['errmsg'],'error');
		}
		if($result['openid']){
			$_SESSION['openid']=$result['openid'];
			$_SESSION['session_key']=$result['session_key'];
			$mid=false;
			if($result['unionid']){
				$_SESSION['unionid']=$result['unionid'];
				$mid=pdo_getcolumn('core_members',array('unionid'=>$result['unionid'],'uniacid'=>$_SESSION['uniacid']),'id');
			}
			$fans=pdo_get('core_fans',array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'ttgid'=>$_SESSION['ttapp_id']),array('id','mid'));
			if($fans){
				if(!$mid && $fans['mid']){
					$mid= $fans['mid'];
				}
			}else{
				$fansdata=array(
					'uniacid'=>$_SESSION['uniacid'],
					'ttgid'=>$_SESSION['ttapp_id'],
					'openid'=>$_SESSION['openid'],
					'type'=>8,
					'createtime' => TIMESTAMP,
				);
				
				if($mid){
					$fansdata['mid']=$mid;
				}
				if($result['unionid']){
					$fansdata['unionid']=$result['unionid'];
				}
				pdo_insert('core_fans',$fansdata);
			}
			if($mid){
				model('core/member')->login($mid);
			}
		}
		$sid=session_id();
		if(!$_SESSION['base_key_str']){
			$_SESSION['base_key_str']=str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_');
		}
		$return=array('ssid'=>$sid,'session_key'=>$_SESSION['session_key'],'openid'=>$_SESSION['openid'],'uid'=>$_SESSION['uid'],'token'=>$_SESSION['token'],'base_key_str'=>$_SESSION['base_key_str']);
		if($wxapp['plugin_name'] && $wxapp['plugin_name'] !='core'){
			$_SESSION['current_plugin']=$wxapp['plugin_name'];
			$file=ROOT_PLUGIN.$wxapp['plugin_name'].'/model/custom_mobile_menu.php';
			if(is_file($file)){
				$return['menus']=plugin($wxapp['plugin_name'])->custom_mobile_menu();
			}
		}
		exi($return);
	}
}